<?php
require_once 'classes/Database.php';
require_once 'classes/OrderManager.php';
require_once 'classes/CDKManager.php';

$orderManager = new OrderManager();
$cdkManager = new CDKManager();

$orderNo = $_GET['order_no'] ?? '';
$order = null;
$cdkInfo = null;

if ($orderNo) {
    $order = $orderManager->getOrderByNo($orderNo);
    if ($order) {
        $cdkInfo = $cdkManager->getCDKByOrderId($order['id']);
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>订单查询 - CDK支付系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .search-box {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 15px;
            padding: 30px;
            margin-bottom: 30px;
        }
        .order-card {
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .status-badge {
            font-size: 0.9rem;
            padding: 8px 15px;
            border-radius: 20px;
        }
        .cdk-display {
            background: linear-gradient(45deg, #4facfe, #00f2fe);
            color: white;
            border-radius: 10px;
            padding: 20px;
            font-size: 1.3rem;
            font-weight: bold;
            letter-spacing: 2px;
            text-align: center;
            margin: 15px 0;
        }
    </style>
</head>
<body>
    <!-- 导航 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
            <a class="navbar-brand" href="index.php">
                <i class="fas fa-shopping-cart me-2"></i>CDK支付系统
            </a>
            <a href="index.php" class="btn btn-outline-light">
                <i class="fas fa-home me-2"></i>返回首页
            </a>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <!-- 搜索框 -->
                <div class="search-box text-center">
                    <h2 class="mb-4">
                        <i class="fas fa-search me-2"></i>订单查询
                    </h2>
                    <form method="GET" class="row g-3 justify-content-center">
                        <div class="col-md-8">
                            <div class="input-group input-group-lg">
                                <input type="text" class="form-control" name="order_no" 
                                       placeholder="请输入订单号" value="<?php echo htmlspecialchars($orderNo); ?>" required>
                                <button class="btn btn-light" type="submit">
                                    <i class="fas fa-search me-2"></i>查询
                                </button>
                            </div>
                        </div>
                    </form>
                </div>

                <?php if ($orderNo && !$order): ?>
                    <!-- 订单不存在 -->
                    <div class="card order-card">
                        <div class="card-body text-center py-5">
                            <i class="fas fa-exclamation-triangle fa-4x text-warning mb-3"></i>
                            <h3 class="text-warning">订单不存在</h3>
                            <p class="lead">未找到订单号：<?php echo htmlspecialchars($orderNo); ?></p>
                            <p>请检查订单号是否正确，或联系客服</p>
                        </div>
                    </div>
                <?php elseif ($order): ?>
                    <!-- 订单信息 -->
                    <div class="card order-card">
                        <div class="card-header bg-primary text-white">
                            <div class="d-flex justify-content-between align-items-center">
                                <h4 class="mb-0">
                                    <i class="fas fa-receipt me-2"></i>订单详情
                                </h4>
                                <?php
                                $statusColors = [
                                    'pending' => 'warning',
                                    'completed' => 'success',
                                    'failed' => 'danger',
                                    'cancelled' => 'secondary'
                                ];
                                $statusTexts = [
                                    'pending' => '待支付',
                                    'completed' => '已完成',
                                    'failed' => '支付失败',
                                    'cancelled' => '已取消'
                                ];
                                ?>
                                <span class="status-badge bg-<?php echo $statusColors[$order['status']]; ?>">
                                    <?php echo $statusTexts[$order['status']]; ?>
                                </span>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <p><strong>订单号：</strong><?php echo htmlspecialchars($order['order_no']); ?></p>
                                    <p><strong>商品名称：</strong><?php echo htmlspecialchars($order['product_name']); ?></p>
                                    <p><strong>购买数量：</strong><?php echo $order['quantity']; ?></p>
                                </div>
                                <div class="col-md-6">
                                    <p><strong>支付金额：</strong>¥<?php echo number_format($order['total_amount'], 2); ?></p>
                                    <p><strong>联系方式：</strong><?php echo htmlspecialchars($order['contact_info']); ?></p>
                                    <p><strong>下单时间：</strong><?php echo date('Y-m-d H:i:s', strtotime($order['created_at'])); ?></p>
                                </div>
                            </div>

                            <?php if ($order['payment_no']): ?>
                                <div class="row mt-3">
                                    <div class="col-12">
                                        <p><strong>支付流水号：</strong><?php echo htmlspecialchars($order['payment_no']); ?></p>
                                    </div>
                                </div>
                            <?php endif; ?>

                            <?php if ($order['status'] === 'completed' && $cdkInfo): ?>
                                <!-- CDK码显示 -->
                                <div class="row mt-4">
                                    <div class="col-12">
                                        <h5 class="mb-3">
                                            <i class="fas fa-gift me-2"></i>CDK码
                                        </h5>
                                        <div class="cdk-display">
                                            <?php echo htmlspecialchars($cdkInfo['cdk_code']); ?>
                                        </div>
                                        <div class="text-center">
                                            <button class="btn btn-primary" onclick="copyCDK()">
                                                <i class="fas fa-copy me-2"></i>复制CDK码
                                            </button>
                                        </div>
                                        <div class="alert alert-warning mt-3">
                                            <i class="fas fa-exclamation-triangle me-2"></i>
                                            <strong>重要提示：</strong>请妥善保管CDK码，遗失不补。
                                        </div>
                                    </div>
                                </div>
                            <?php elseif ($order['status'] === 'pending'): ?>
                                <!-- 待支付提示 -->
                                <div class="alert alert-info mt-3">
                                    <i class="fas fa-info-circle me-2"></i>
                                    订单尚未支付，<a href="buy.php?id=<?php echo $order['product_id']; ?>" class="alert-link">点击这里继续支付</a>
                                </div>
                            <?php elseif ($order['status'] === 'failed'): ?>
                                <!-- 支付失败提示 -->
                                <div class="alert alert-danger mt-3">
                                    <i class="fas fa-times-circle me-2"></i>
                                    支付失败，<a href="buy.php?id=<?php echo $order['product_id']; ?>" class="alert-link">点击这里重新购买</a>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>

                    <!-- 操作按钮 -->
                    <div class="text-center mt-4">
                        <a href="index.php" class="btn btn-primary me-2">
                            <i class="fas fa-home me-2"></i>返回首页
                        </a>
                        <?php if ($order['status'] === 'pending'): ?>
                            <a href="buy.php?id=<?php echo $order['product_id']; ?>" class="btn btn-success me-2">
                                <i class="fas fa-credit-card me-2"></i>继续支付
                            </a>
                        <?php endif; ?>
                        <button class="btn btn-outline-secondary" onclick="window.print()">
                            <i class="fas fa-print me-2"></i>打印订单
                        </button>
                    </div>
                <?php endif; ?>

                <!-- 查询说明 -->
                <?php if (!$orderNo): ?>
                    <div class="card mt-4">
                        <div class="card-body">
                            <h5><i class="fas fa-info-circle me-2"></i>查询说明</h5>
                            <ul class="mb-0">
                                <li>请输入完整的订单号进行查询</li>
                                <li>订单号通常在支付页面或支付成功页面显示</li>
                                <li>如果忘记订单号，请联系客服提供联系方式进行查询</li>
                                <li>支付成功后，CDK码将显示在订单详情中</li>
                            </ul>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        function copyCDK() {
            const cdkCode = document.querySelector('.cdk-display').textContent.trim();
            navigator.clipboard.writeText(cdkCode).then(function() {
                // 显示复制成功提示
                const btn = event.target;
                const originalText = btn.innerHTML;
                btn.innerHTML = '<i class="fas fa-check me-2"></i>已复制';
                btn.disabled = true;
                
                setTimeout(function() {
                    btn.innerHTML = originalText;
                    btn.disabled = false;
                }, 2000);
            }).catch(function(err) {
                alert('复制失败，请手动复制：' + cdkCode);
            });
        }
    </script>
</body>
</html>
